/*
挂起与恢复
    1.使用wait()可挂起当前线程
    2.使用notify()可恢复当前线程
    3.synchronized锁定要监视(操作)的线程,然后通过wait(),notify()进行线程操作
 */
public class Project {
    public static void main(String[] args) throws InterruptedException {
        NewThread thread1 = new NewThread("Hello");
        NewThread thread2 = new NewThread("world");
        synchronized(thread1) {
            try {
                thread1.setPauseSta(true);
                Thread.sleep(3000);
                thread1.setPauseSta(false);
            }catch (Exception e){
                System.out.println("main:"+e);
            }
        }

        thread1.thread.join();
        thread2.thread.join();
        System.out.println("main end");
    }
}


class NewThread implements Runnable{
    Thread thread;
    String name;
    volatile boolean pauseSta;
    NewThread(String name){
        thread = new Thread(this,name);
        this.name = name;
        pauseSta = false;
        thread.start();
    }
    public void run(){
        for (int i = 0;i<10;i++){
            System.out.println(name+":"+i);
            try {
                Thread.sleep(1000);
                synchronized (this) {
                    while (pauseSta) {
                        this.wait();
                    }
                }
            }catch (Exception e){
                System.out.println(name+":"+e);
            }

        }
        System.out.println(name+":out");
    }
    public void setPauseSta(boolean pauseSta){
        if (pauseSta){
            this.pauseSta = true;
        }
        else {
            this.pauseSta = false;
            this.notify();
        }
    }

}
